package src.main.java.org.annotation.demo;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/*
* 模拟Junit框架
* 定义若干个方法，只要加上MyTest注解，就会触发该方法执行
* */
public class AnnotaionTest {
    @MyTest
    public void test1(){
        System.out.println("test1");
    }
    @MyTest
    public void test2(){
        System.out.println("test2");
    }
    public void test3(){
        System.out.println("test3");
    }
    public void test4(){
        System.out.println("test4");
    }

    public static void main(String[] args) throws Exception {
        AnnotaionTest annotaionTest = new AnnotaionTest();
        //获取Class对象
        Class annotaionTestClass = AnnotaionTest.class;
        //获取类中的全部方法
        Method[] declaredMethods = annotaionTestClass.getDeclaredMethods();
        for (Method declaredMethod : declaredMethods) {
            //判断方法上是否有注解
            boolean annotationPresent = declaredMethod.isAnnotationPresent(MyTest.class);
            if(annotationPresent){
                //有注解就执行
                declaredMethod.invoke(annotaionTest);
            }
        }
    }
}
